import { renderAmis } from 'utils/amisUtil'
import { encryptedPassword, encrypt } from 'utils'
import allBus from 'core/bus'

function Login() {
  return renderAmis({
    type: 'page',
    style: {
      background: '#e9e9eb'
    },
    body: {
      type: 'form',
      title: '商户管理后台',
      api: {
        method: 'post',
        url: '/manager/login/v1',
        data: {
          phone: '$phone',
          password: '$password'
        },
        requestAdaptor: function (api, context) {
          return {
            ...api,
            data: {
              ...api.data, // 获取暴露的 api 中的 data 变量
              password: encrypt(encryptedPassword(api.data.password)) // 新添加数据
            }
          }
        },
        adaptor: function (payload) {
          if (payload.code === 0) {
            allBus.common.loginSuccess.emit(payload.data)
          }
          return {
            ...payload
          }
        }
      },
      submitText: '登陆',
      style: {
        width: '400px',
        position: 'absolute',
        top: '50%',
        right: '100px',
        transform: 'translateY(-50%)'
      },
      body: [
        {
          type: 'input-text',
          placeholder: '请输入手机号',
          name: 'phone'
        },
        {
          type: 'input-password',
          placeholder: '请输入密码',
          name: 'password'
        }
      ]
    }
  })
}

export default Login
